package com.gitee.es.dynamics.config;

import com.gitee.es.dynamics.common.ContextHolderInit;
import com.gitee.es.dynamics.common.ElasticSearchConfig;
import com.gitee.es.dynamics.context.DynamicsElasticSearchContextHolder;
import com.gitee.es.dynamics.util.CoreUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * sql配置抽象类
 */
@Slf4j
public abstract class AbstractElasticSearchSqlConfig implements ContextHolderInit {
    @Autowired
    private DynamicsElasticSearchConfig dynamicsElasticSearchConfig;

    /**
     * 加载es sql配置
     *
     * @return java.util.List<com.gitee.es.dynamics.common.ElasticSearchConfig>
     * @date 2023/12/29 8:45
     * @author glq
     */
    public abstract List<ElasticSearchConfig> loadElasticSearchSqlConfig();

    /**
     * 初始化 es sql配置
     *
     * @return void
     * @date 2023/12/29 8:47
     * @author glq
     */
    @Override
    @PostConstruct
    public void init() {
        DynamicsElasticSearchContextHolder.setElasticSearchSqlConfig(this);
        if (dynamicsElasticSearchConfig.getOpenSqlConfig()) {
            log.info("Use user sql config!");
            List<ElasticSearchConfig> configs = loadElasticSearchSqlConfig();
            CoreUtil.loadCoreConfig(configs);
        }

    }
}
